package com.css.fxfzxczh.modules.report.controller;

import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.report.entity.ReportTemplate;
import com.css.fxfzxczh.modules.report.service.ReportTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/reportTemplate")
public class ReportTemplateController {
    @Autowired
    private ReportTemplateService reportTemplateService;

    //获取报告模板编号
    @GetMapping("/getNumber")
    public RestResponse getNumber() {
        RestResponse restResponse = null;
        try {
            String number = reportTemplateService.getNumber();
            restResponse = RestResponse.succeed(number);
        } catch (Exception e) {
            String errormessage = "获取编号生成失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //添加模板
    @PostMapping("/save")
    public RestResponse save(@RequestBody ReportTemplate reportTemplate) {
        RestResponse restResponse = null;
        try {
            reportTemplateService.save(reportTemplate);
            restResponse = RestResponse.succeed();
        } catch (Exception e) {
            String errormessage = "添加报告模板失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //获取所有模板
    @GetMapping("/getList")
    public RestResponse getList(){
        RestResponse restResponse = null;
        try {
            List<ReportTemplate> list = reportTemplateService.getList();
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            String errormessage = "获取所有报告模板失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //上传 替换模板
    @PostMapping("/updateTemplate")
    public RestResponse updateTemplate(@RequestBody ReportTemplate reportTemplate){
        RestResponse restResponse = null;
        try {
            reportTemplateService.updateTemplate(reportTemplate);
            restResponse = RestResponse.succeed();
        } catch (Exception e) {
            String errormessage = "上传模板失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
